<?php

namespace app\admin\controller;
use app\common\model\Category;
use think\Controller;

use think\Request;
use houdunwang\arr\Arr;
use app\common\model\Goods as GoodsModel;
class Goods extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = GoodsModel::select();
        return view('',compact('data'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        #查询栏目数据
        $cData = Category::select();
        #以树状呈现
        $cData = Arr::tree($cData,'cname','id','pid');
        return view('',compact('cData'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(GoodsModel $goods)
    {
        if (request()->isPost())
        {
            $post = input('post.');
            $res = $goods->add($post);
            if ($res['valid']==1)
            {
                return $this->success($res['message']);
            }else
            {
                return $this->error($res['message']);
            }
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        #查询栏目数据
        $cData=db('category')->select();
        $cData = Arr::tree($cData,'cname','id','pid');
        #查询其他数据goods表数据
        $gData=db('goods')->find($id);
//        halt($gData);
        #查询商品属性表(goodsattribute)
        $attrData = db('goodsattribute')->where('goods_id',$id )->select();
//        halt($attrData);

        foreach ($attrData as $k =>$v)
        {
            #查询商品图片表(goodspic)
            $picData = db('goodspic')->where('attributeid', $v['id'])->select();
//            halt($picData);
            $Data [$k] =$picData[$k]['pic'];
            $repertory = $v['repertory'];
            $size = $v['size'];
            $color = $v['color'];
            $attrData[$k] =[
                'color'=>$color,
                'pic'=>$Data,
                'repertory'=>$repertory,
                'size'=>$size,

            ];
        }

        $attrData = json_encode($attrData);
//        halt($attrData);
        return view('',compact('cData','gData','id','attrData'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        halt($_POST);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        return $res = db('goods')->delete($id);
    }
}
